#include "magc.h"

static xSemaphoreHandle magcSema ;

static void magc_track_isr(void)
{
    xSemaphoreGiveFromISR( magcSema, NULL );
}

void magc_task( void *pvParameters )
{
	mcr_decoded_track_t decoded_track[3];
	u8 track_index=0;

	mcr_init();
	mcr_set_complete_callback(magc_track_isr);
	
	magcSema =  xSemaphoreCreateMutex();
	xSemaphoreTake(magcSema, 0);
	 
	while(1){
		mcr_init_track(1);
		mcr_init_track(2);
		mcr_init_track(3);

		while (xSemaphoreTake( magcSema, 0xFFFF ) != pdTRUE);
		mcr_track_decode(1,&decoded_track[0]);
		mcr_track_decode(2,&decoded_track[1]);
		mcr_track_decode(3,&decoded_track[2]);

		mcr_disable();
		//mcr_enable();
		 
		// send to cryto....
		for(track_index=0; track_index < MCR_NUM_TRACKS ; track_index++){
			if (decoded_track[track_index].error_code != MCR_ERR_OK){
				magc_debug("[MAGC] track[%d]: ERR  -> %d \n", (track_index+1), decoded_track[track_index].data);
				magc_debug("[MAGC] track err\n");

				mcr_init_track(track_index+1);
			}
			else{
				magc_debug("[MAGC] track[%d]: DATA-> %s \n", (track_index+1), decoded_track[track_index].data);
				magc_debug("[MAGC] track ok\n");
			}
		}
	}
}









